package com.example.lightoftheparty;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class BettleActivity extends Activity {
	
	int rc = 0;  //redcount
	int bc = 0;  //bluecount

	Button[] btn = new Button[2];
	
	TextView timer,redcount,bluecount;
	TextView startcount;
	
	View redbtn1,redbtn2,bluebtn1,bluebtn2;//redbtn1 = 빨강오른쪽 redbtn2 = 빨강왼쪽
	View rw,bw; // redwin,bluewin
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_bettle);
	    
	    timer = (TextView)findViewById(R.id.timer);
	    redcount = (TextView)findViewById(R.id.redcount);
	    bluecount = (TextView)findViewById(R.id.bluecount);
	    
	    startcount = (TextView)findViewById(R.id.startcount);
	    
	    redbtn1 = (Button)findViewById(R.id.redbutton1);
	    redbtn2 = (Button)findViewById(R.id.redbutton2);
	    bluebtn1 = (Button)findViewById(R.id.bluebutton1);
	    bluebtn2 = (Button)findViewById(R.id.bluebutton2);
	    
	    rw = (Button)findViewById(R.id.redwin);
	    bw = (Button)findViewById(R.id.bluewin);
	    
	    btn[0] = (Button)findViewById(R.id.redwin);
	    btn[1] = (Button)findViewById(R.id.bluewin);

	    for(int i=0; i<btn.length; i++) {
			btn[i].setOnClickListener(mListener);
		}
	    
	    handler.sendEmptyMessage(0);
	    
	    // TODO Auto-generated method stub
	}
	private int start = 4;
	private int time = 30;
	private TextView textView = null;
	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if(start>0) {
				start--;
				startcount.setText(""+start);
			}
			if(start == 0 && time>0) {
			startcount.setVisibility(View.INVISIBLE);
			time--;
			timer.setText(""+time);
			}
			handler.sendEmptyMessageDelayed(0,1000);
			if((rc > bc) && time == 0) {
				rw.setVisibility(View.VISIBLE);
			}
			else if((bc > rc) && time == 0) {
				bw.setVisibility(View.VISIBLE);
			}
		}
	};
	public void gored(View v) {
		if(time>0 && start == 0) {
		rc++;
		redcount.setText(""+rc);
		}
	}
	public void goblue(View v) {
		if(time>0 && start == 0) {
		bc++;
		bluecount.setText(""+bc);
		}
	}
OnClickListener mListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.redwin :
				onBackPressed();
				break;
			case R.id.bluewin :
				onBackPressed();
				break;
			}
	}
	};
	
}
